package com.diana.serviceImpl;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.diana.common.StatusEnums;
import com.diana.common.Utils;
import com.diana.entity.ManagerMenu;
import com.diana.mapper.ManagerMenuMapper;
import com.diana.service.ManagerMenuService;
import com.diana.service.QueueMainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author anzhi
 * @since 2019-04-21
 */
@Service
public class ManagerMenuServiceImpl extends ServiceImpl<ManagerMenuMapper, ManagerMenu> implements ManagerMenuService {

    @Autowired
    private QueueMainService queueMainService;
    @Autowired
    private ManagerMenuMapper managerMenuMapper;

    /**
     * 新增
     * @param map
     * @return
     */
    @Override
    public String addMenu(Map<String, Object> map) {
        String menuName = Utils.toString(map.get("menuName"));
        String parentId = Utils.toString(map.get("parentId"));
        Integer level = Utils.toInteger(map.get("level"));
        String permissionUrl = Utils.toString(map.get("permissionUrl"));
        String remark = Utils.toString(map.get("remark"));
        String url = Utils.toString(map.get("url"));
        if(Utils.isNullOrEmpty(menuName)){
            return Utils.message("0001","menuName不能为空",null);
        }
        if(level==null){
            return Utils.message("0001","level不能为空",null);
        }
        ManagerMenu info = selectOne(Condition.create().eq("menuName",menuName).ne("status",StatusEnums.CommonStatus.删除));
        if(info!=null){
            return Utils.message("0001","menuName已存在，请修改",null);
        }
        ManagerMenu managerMenu = new ManagerMenu();
        if(level== StatusEnums.MenuType.菜单){
            if(Utils.isNullOrEmpty(url)){
                return Utils.message("0001","menu为菜单时url不能为空",null);
            }
            managerMenu = selectOne(Condition.create().eq("url", url).ne("status", StatusEnums.CommonStatus.删除));
            if(managerMenu!=null){
                return Utils.message("0001","该url已存在",null);
            }
        }
        if(level==StatusEnums.MenuType.按钮){
            if(Utils.isNullOrEmpty(permissionUrl)){
                return Utils.message("0001","menu为按钮时permissionUrl不能为空",null);
            }
            managerMenu = selectOne(Condition.create().eq("permissionUrl", permissionUrl).ne("status", StatusEnums.CommonStatus.删除));
            if(managerMenu!=null){
                return Utils.message("0001","该permissionUrl已存在",null);
            }
        }
        managerMenu = new ManagerMenu();
        String menuId = "";
        String queueName = "menu"+parentId;
        String queueValue = queueMainService.getQueueValueNoPre(queueName);
        if(queueValue!=null&&queueValue.contains("服务器拥挤")){
            return queueValue;
        }
        if(Utils.isNullOrEmpty(queueValue)){
            //当queueValue为空时创建序列
            Map<String,Object> queueMap = new HashMap<>();
            queueMap.put("queueName",queueName);
            queueMap.put("queueAlias","menu");
            queueMap.put("queuePrefix","menu");
            queueMap.put("queueValue",1);
            String result = queueMainService.createQueue(queueMap);
            if(!result.contains("0000")){
                return result;
            }
            menuId = (("0").equals(parentId)?"":parentId)+"01";
        }else{
            menuId = (("0").equals(parentId)?"":parentId)+Utils.getSerialNum(queueValue,2);
        }
        managerMenu.setMenuId(menuId);
        managerMenu.setParentId(parentId==null?"0":parentId);
        managerMenu.setLevel(level);
        managerMenu.setMenuName(menuName);
        managerMenu.setPermissionUrl(permissionUrl);
        managerMenu.setRemark(remark);
        managerMenu.setStatus(StatusEnums.CommonStatus.可用);
        managerMenu.setUrl(url);
        if(insert(managerMenu)){
            return Utils.message("0000","操作成功",null);
        }
        return Utils.message("0001","操作失败",null);
    }

    /**
     * 修改
     * @param map
     * @return
     */
    @Override
    public String updateMenu(Map<String, Object> map) {
        String menuId = Utils.toString(map.get("menuId"));
        String menuName = Utils.toString(map.get("menuName"));
        String permissionUrl = Utils.toString(map.get("permissionUrl"));
        String remark = Utils.toString(map.get("remark"));
        String url = Utils.toString(map.get("url"));
        Integer status = Utils.toInteger(map.get("status"));
        if(Utils.isNullOrEmpty(menuId)){
            return Utils.message("0001","menuId不能为空",null);
        }
        ManagerMenu managerMenu = selectOne(Condition.create().eq("menuId", menuId).ne("status", StatusEnums.CommonStatus.删除));
        if(managerMenu==null){
            return Utils.message("0001","数据不存在",null);
        }
        if(!Utils.isNullOrEmpty(menuName)){
            ManagerMenu info = selectOne(Condition.create().eq("menuName",menuName).ne("status",StatusEnums.CommonStatus.删除).ne("menuId",menuId));
            if(info!=null){
                return Utils.message("0001","menuName已存在，请修改",null);
            }
        }
        if(!Utils.isNullOrEmpty(permissionUrl)){
            ManagerMenu info = selectOne(Condition.create().eq("permissionUrl", permissionUrl).ne("status", StatusEnums.CommonStatus.删除).ne("menuId",menuId));
            if(info!=null){
                return Utils.message("0001","permissionUrl已存在，请修改",null);
            }
        }
        if(!Utils.isNullOrEmpty(url)){
            ManagerMenu info = selectOne(Condition.create().eq("url", url).ne("status", StatusEnums.CommonStatus.删除).ne("menuId",menuId));
            if(info!=null){
                return Utils.message("0001","url已存在，请修改",null);
            }
        }
        managerMenu = new ManagerMenu();
        managerMenu.setMenuId(menuId);
        managerMenu.setMenuName(menuName);
        managerMenu.setPermissionUrl(permissionUrl);
        managerMenu.setRemark(remark);
        managerMenu.setUrl(url);
        managerMenu.setStatus(status);
        boolean num = updateById(managerMenu);
        if(num){
            return Utils.message("0000","操作成功",null);
        }
        return Utils.message("0001","操作失败",null);
    }

    /**
     * 删除
     * @param map
     * @return
     */
    @Override
    public String updateStatus(Map<String, Object> map) {
        String menuId = Utils.toString(map.get("menuId"));
        Integer status = Utils.toInteger(map.get("status"));
        if(Utils.isNullOrEmpty(menuId)){
            return Utils.message("0001","menuId不能为空",null);
        }
        if(status==null){
            return Utils.message("0001","status不能为空",null);
        }
        ManagerMenu managerMenu = selectOne(Condition.create().eq("menuId", menuId).ne("status", StatusEnums.CommonStatus.删除));
        if(managerMenu==null){
            return Utils.message("0001","数据不存在",null);
        }
        int count = selectCount(Condition.create().eq("parentId", menuId).ne("status", StatusEnums.CommonStatus.删除));
        if(count>0){
            return Utils.message("0001","存在子菜单，请先删除子菜单",null);
        }
        managerMenu = new ManagerMenu();
        managerMenu.setMenuId(menuId);
        managerMenu.setStatus(status);
        if(updateById(managerMenu)){
            return Utils.message("0000","操作成功",null);
        }
        return Utils.message("0001","操作失败",null);
    }

    /**
     * 递归获取树形列表
     * @param map
     * @return
     */
    @Override
    public String getMenuList(Map<String, Object> map) {
        String parentId = Utils.toString(map.get("parentId"));
        Integer status  = Utils.toInteger(map.get("status"));
        String roleId = Utils.toString(map.get("roleId"));
        String flag = Utils.toString(map.get("flag"));
        List<Map<String, Object>> list = getListRecursion(parentId, status, roleId,flag);
        //当查询父级菜单时,封装一级菜单
        if(!Utils.isNullOrEmpty(flag)&&"parent".equals(flag)){
            Map<String,Object> info = new HashMap<>();
            info.put("child",list);
            info.put("menuId","0");
            info.put("menuName","一级菜单");
            list = new ArrayList<>();
            list.add(info);
        }
        return Utils.message("0000","获取成功",list);
    }

    public List<Map<String,Object>> getListRecursion(String parentId,Integer status,String roleId,String flag){
        List<Map<String,Object>> list = new ArrayList<>();
        if(Utils.isNullOrEmpty(parentId)){
            parentId = "0";
        }
        String joinsql = "";
        if(status==null){
            joinsql += " and a.status != 2";
        }else {
            joinsql += " and a.status = "+status+" ";
        }
        if(!Utils.isNullOrEmpty(flag)&&"parent".equals(flag)){
            joinsql += " and a.level != 2 ";
        }
        if(Utils.isNullOrEmpty(roleId)){
            list = managerMenuMapper.getList(parentId, joinsql);
            for(Map<String,Object> map:list){
                String childIds = Utils.toString(map.get("childIds"));
                String menuId = Utils.toString(map.get("menuId"));
                if(!Utils.isNullOrEmpty(childIds)){
                    List<Map<String, Object>> maps = getListRecursion(menuId, status, null,flag);
                    map.put("child",maps);
                }else{
                    map.put("child",new ArrayList<>());
                }
            }
        }else{

        }
        return list;
    }

    /**
     * 获取菜单info
     * @param map
     * @return
     */
    @Override
    public String getMenuInfo(Map<String,Object> map) {
        String menuId = Utils.toString(map.get("menuId"));
        if(Utils.isNullOrEmpty(menuId)){
            return Utils.message("0001","请选择menuId",null);
        }
        Map<String, Object> menuInfo = managerMenuMapper.getMenuInfo(menuId);

        return Utils.message("0000","获取成功",menuInfo);
    }

    /**
     * 获取所有父级菜单
     * @param map
     * @return
     */
    @Override
    public String getParentList(Map<String, Object> map) {
        return null;
    }
}
